package com.szt.mobile.inventory.common.utils;

import android.os.Handler;
import android.os.Looper;
import android.text.TextUtils;

import com.szt.mobile.inventory.app.HandsetApplication;

import org.json.JSONObject;
import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

/**
 * @author 刘育成
 * @package com.lyc.xutilsdemo
 * @fileName Xutils
 * @date or 2019/1/7 9:37
 * @describe TODO
 */
public class Xutils {
    private volatile static Xutils instance;
    private Handler handler;

    private static final String CODE_PARAMS= "httpCode";
    private static final int SUCCESS = 200;//code返回成功；
    private static final int INTERNAL_SERVER_ERROR = 500;


    private Xutils() {
        handler = new Handler(Looper.getMainLooper());
    }

    /**
     * 单例模式
     *
     * @return
     */
    public static Xutils getInstance() {
        if (instance == null) {
            synchronized (Xutils.class) {
                if (instance == null) {
                    instance = new Xutils();
                }
            }
        }
        return instance;
    }
    /**
     * 异步post请求
     *
     * @param url
     * @param json
     * @param callback
     */
    public void post(String url, String json, final XCallBack callback){
        RequestParams params = new RequestParams(url);
        params.addHeader("Content type","application/json");
        params.setBodyContent(json);
        params.setCacheMaxAge(1000 * 60);
        x.http().post(params, new Callback.CommonCallback<String>() {
            private String result=null;
            @Override
            public void onSuccess(String result) {
                if (result!=null){
                    this.result=result;
                }
//            onSuccessResponse(result,callback);

            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                callback.onError(ex.getMessage());

            }

            @Override
            public void onCancelled(CancelledException cex) {
                callback.onError(cex.getMessage());
            }

            @Override
            public void onFinished() {
                if (result!=null){
                    JSONObject jsonObject=null;
                    try {
                        jsonObject=new JSONObject(result);
                        int code=jsonObject.getInt(CODE_PARAMS);
                        if (code==SUCCESS){
                            onSuccessResponse(result,callback);
                        }else if (code==INTERNAL_SERVER_ERROR){
                            callback.onError("未连接网络");
                        }else {

                            callback.onError("error");
                        }
                    }catch (Exception e){
                        e.printStackTrace();
                    }
                }

            }
        });
    }

    /**
     * 异步get请求返回结果,json字符串
     *
     * @param result
     * @param callBack
     */
    private void onSuccessResponse(final String result, final XCallBack callBack) {
        handler.post(new Runnable() {
            @Override
            public void run() {
                if (callBack != null) {
                    callBack.onResponse(result);
                }
            }
        });
    }


    public interface XCallBack {
        void onResponse(String result);
        void onError(String e);
    }
}
